      1  import java.util.Scanner;

      2

      3  /**

      4     This program demonstrates a decision tree for an animal

      5     guessing game.

      6  */

      7  public class DecisionTreeDemo

      8  {

      9     public static void main(String[] args)

     10     {

     11        BinaryTree questionTree = new BinaryTree("Is it a mammal?",

     12           new BinaryTree("Does it have stripes?",

     13              new BinaryTree("Is it a carnivore?",

     14                 new BinaryTree("It is a tiger."),

     15                 new BinaryTree("It is a zebra.")),

     16              new BinaryTree("It is a pig.")),

     17           new BinaryTree("Does it fly?",

     18              new BinaryTree("It is an eagle."),

     19              new BinaryTree("Does it swim?",

     20                 new BinaryTree("It is a penguin."),

     21                 new BinaryTree("It is an ostrich."))));

     22

     23        boolean done = false;

     24        Scanner in = new Scanner(System.in);

     25        while (!done)

     26        {

     27           BinaryTree left = questionTree.left();

     28           BinaryTree right = questionTree.right();

     29           if (left.isEmpty() && right.isEmpty())

     30           {

     31              System.out.println(questionTree.data());

     32              done = true;

     33           }

     34           else

     35           {

     36              String response;

     37              do

     38              {

     39                 System.out.print(questionTree.data() + " (Y/N) ");

     40                 response = in.next().toUpperCase();

     41              }

     42              while (!response.equals("Y") && !response.equals("N"));

     43

     44              if (response.equals("Y"))

     45              {

     46                 questionTree = left;

     47              }

     48              else

     49              {

     50                 questionTree = right;

     51              }

     52           }

     53        }

     54     }

     55  }